package com.ebelter.test01;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IConnectStationCallback;
import com.ebelter.btcomlib.models.bluetooth.products.dianji8.bean.Dj8MesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.dianji8.bean.Dj8ScaleUserInfo;
import com.ebelter.btcomlib.models.bluetooth.products.dianji8.interfaces.Dj8MeasureCallback;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.test01.module.dj8module.Dj8ScaleBleManager;
import com.holtek.libHTBodyfat.HTBodyBasicInfo;
import com.holtek.libHTBodyfat.HTBodyResultAllBody;
import com.holtek.libHTBodyfat.HTBodyResultTwoLegs;

public class MainActivity extends Activity {

    private static final String TAG = "CESHIMainActivity";
    Dj8ScaleBleManager dj8ScaleBleManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dj8ScaleBleManager = new Dj8ScaleBleManager(this);

        Dj8ScaleUserInfo userInfo = new Dj8ScaleUserInfo();
        userInfo.setUserId(0x01);
        userInfo.setAge(20);
        userInfo.setRole(0);//0普通 1运动员
        userInfo.setHeight(170);
        userInfo.setWeight(65.4f);
        userInfo.setSex(0);//0男1女
        dj8ScaleBleManager.setScaleUserInfo(userInfo);

        dj8ScaleBleManager.setConnectStationCallback(new IConnectStationCallback() {
            @Override
            public void onConnected(ProductStyle productStyle, BluetoothDevice device) {

            }

            @Override
            public void onConnecting(ProductStyle productStyle, BluetoothDevice device) {

            }

            @Override
            public void onDisConnected(ProductStyle productStyle) {

            }
        });

        dj8ScaleBleManager.setScaleMeasureCallback(new Dj8MeasureCallback() {
            @Override
            public void onScaleWake(byte b0,byte b1) {

            }

            @Override
            public void onScaleSleep() {

            }

            @Override
            public void onPowLow() {

            }

            @Override
            public void receiveTime(int model, String time, long time2) {

            }

            @Override
            public void setUserInfoSuccess() {

            }

            @Override
            public void onReceiveMeasureResult(Dj8MesureResult result) {

            }

            @Override
            public void onReceiveHisMeasureResult(Dj8MesureResult result) {

            }

            @Override
            public void getHisDataFinish() {

            }
        });
    }

    public void start(View view) {
        dj8ScaleBleManager.startWork();
    }

    public void stop(View view) {
        dj8ScaleBleManager.stopWork();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        dj8ScaleBleManager.exit();
        dj8ScaleBleManager=null;
    }

    //测试
    public void ceshi(View view) {

        int height = 169;
        float weight = 50.3f;
        int age = 25;
        int sex = 0;
        if (sex==0){
            sex = HTBodyBasicInfo.SexTypeMale;
        }else {
            sex = HTBodyBasicInfo.SexTypeFemale;
        }

        Dj8MesureResult result=new Dj8MesureResult();
        HTBodyBasicInfo basicInfo = new HTBodyBasicInfo(sex,height,weight,age);


        basicInfo.htZAllBodyImpedance  = 500;
        basicInfo.htZLeftLegImpedance  = 259;
        basicInfo.htZRightLegImpedance = 345;
        basicInfo.htZLeftArmImpedance  = 234;
        basicInfo.htZRightArmImpedance = 222;


        int djCount=4;
        if (djCount==4){//4电极数

            basicInfo.htTwoLegsImpedance = 333;//两脚阻抗
            HTBodyResultTwoLegs resultTwolegs = new HTBodyResultTwoLegs();
            int errorType = resultTwolegs.getBodyfatWithBasicInfo(basicInfo);
            result.measureType=errorType;
            if(errorType == HTBodyBasicInfo.ErrorNone){
                //正常计算
                result.bmi = resultTwolegs.htBMI;
                result.bmr = resultTwolegs.htBMR;
                result.vfat = resultTwolegs.htVFAL;//内脏脂肪
                result.boneMass = resultTwolegs.htBoneKg;//骨量
                result.fatP = resultTwolegs.htBodyfatPercentage;//脂肪率
                result.fatKg = resultTwolegs.htBodyfatKg;//脂肪量
                result.waterP = resultTwolegs.htWaterPercentage;//水分率
                result.muscleKg = resultTwolegs.htMuscleKg;//肌肉量
                result.muscleP = resultTwolegs.htMusclePercentage;//肌肉量
                result.bodyAge = resultTwolegs.htBodyAge;//体年龄
                result.bodyScore = resultTwolegs.htBodyScore;//体年龄
                result.proteinP = resultTwolegs.htProteinPercentage;//蛋白质
                result.bodyfatSubcutP = resultTwolegs.htBodyfatSubcut;//皮下脂肪率
                result.bodyfatSubcutKg = resultTwolegs.htBodyfatSubcutKg;//皮下脂肪量
                result.idealWeightKg = resultTwolegs.htIdealWeightKg;//理想體重
                result.bodyfatFreeMass = resultTwolegs.htBodyfatFreeMass;//去脂体重
            }


        }else if (djCount==8){

            HTBodyResultAllBody resultTwolegs = new HTBodyResultAllBody();
            int errorType = resultTwolegs.getBodyfatWithBasicInfo(basicInfo);
            if(errorType == HTBodyBasicInfo.ErrorNone){
                //正常计算
                result.bmi = resultTwolegs.htBMI;
                result.bmr = resultTwolegs.htBMR;
                result.vfat = resultTwolegs.htVFAL;//内脏脂肪
                result.boneMass = resultTwolegs.htBoneKg;//骨量
                result.fatP = resultTwolegs.htBodyfatPercentage;//脂肪率
                result.fatKg = resultTwolegs.htBodyfatKg;//脂肪量
                result.waterP = resultTwolegs.htWaterPercentage;//水分率
                result.muscleKg = resultTwolegs.htMuscleKg;//肌肉量
                result.muscleP = resultTwolegs.htMusclePercentage;//肌肉量
                result.bodyAge = resultTwolegs.htBodyAge;//体年龄
                result.bodyScore = resultTwolegs.htBodyScore;//体年龄
                result.proteinP = resultTwolegs.htProteinPercentage;//蛋白质
                result.bodyfatSubcutP = resultTwolegs.htBodyfatSubcut;//皮下脂肪率
                result.bodyfatSubcutKg = resultTwolegs.htBodyfatSubcutKg;//皮下脂肪量
                result.idealWeightKg = resultTwolegs.htIdealWeightKg;//理想體重
                result.bodyfatFreeMass = resultTwolegs.htBodyfatFreeMass;//去脂体重
            }

        }


        LogUtils.i(TAG,"----result = "+result);

    }


}
